/*
 * This file is part of ksPlatform
 * 
 * Copyright (c) 2014-2015
 * 
 * This file is dual-licensed under the AGPLv3 (http://www.gnu.org/licenses/agpl-3.0.html) 
 * 
 * If you are unsure which license is appropriate for your use, please contact the sales department
 * at 97041437@qq.com.
 */
package cn.com.ksplatform.base;

import java.io.Serializable;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.core.context.PlatformContext;

/**
 * 持久化实体类
 * 
 * BaseModel
 * 
 * 2015年3月17日 上午9:49:02
 * 
 * @author Mr.Xiao
 * @version 1.0.0
 * 
 * 本类只保留增加、修改、删除方法，查询方法在dao基类调用
 * 
 */
public abstract class BaseModel implements Serializable{

	private String sessionFactoryName = "sessionFactory";

	/**
	 * 获取Session
	 * 
	 * @return
	 */
	private Session getCurrentSession() {
		SessionFactory sf = PlatformContext.getBean(sessionFactoryName, SessionFactory.class);
		Session session = sf.getCurrentSession();
		return session;
	}

	/**
	 * 设置Session工程名称 setSessionFactoryName()
	 * 
	 * @param sessionFactoryName
	 * @return BaseModel
	 * @exception
	 * @since 1.0.0
	 */
	public BaseModel setSessionFactoryName(String sessionFactoryName) {
		this.sessionFactoryName = sessionFactoryName;
		return this;
	}

	/**
	 * 
	 * saveOrUpdate()
	 * 
	 * @param flag
	 * @return
	 * @return Object
	 * @exception
	 * @since 1.0.0
	 */
	private Object saveOrUpdate(String flag) {
		Session session = getCurrentSession();
		Object obj = null;
		try {
			if (flag.equals("update")) {
				session.update(this);
			} else if (flag.equals("save")) {
				obj = session.save(this);
			} else if (flag.equals("saveOrUpdate")) {
				session.saveOrUpdate(this);
			} else if (flag.equals("delete")) {
				session.delete(this);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return obj;
	}

	/**
	 * 保存 save()
	 * 
	 * @return
	 * @return Return
	 * @exception
	 * @since 1.0.0
	 */
	public String save() {
		String Id = null;
		try {
			Id = saveOrUpdate("save").toString();
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e.getMessage());
		}
		return Id;
	}

	/**
	 * 更新 update()
	 * @return 
	 * 
	 * @return
	 * @return Return
	 * @exception
	 * @since 1.0.0
	 */
	public <T extends BaseModel> T update() {
		try {
			saveOrUpdate("update");
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e.getMessage());
		}
		return (T)this;
	}

	/**
	 * 新增或更新 saveOrUpdate()
	 * 
	 * @return Return
	 * @exception
	 * @since 1.0.0
	 */
	public <T extends BaseModel> T saveOrUpdate() {
		try {
			saveOrUpdate("saveOrUpdate");
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e.getMessage());
		}
		return (T)this;
	}

	/**
	 * 删除 delete()
	 * 
	 * @return
	 * @return Return
	 * @exception
	 * @since 1.0.0
	 */
	public <T extends BaseModel> T delete() {
		try {
			saveOrUpdate("delete");
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e.getMessage());
		}
		return (T)this;
	}

	/**
	 * 批量保存数据
	 * 
	 * @param <T>
	 * @param entitys
	 *            要持久化的临时实体对象集合
	 *//*
	public void batchsave(List<?> entitys) {
		try {
			Session session = getCurrentSession();
			for (int i = 0; i < entitys.size(); i++) {
				session.save(entitys.get(i));
				if (i % 20 == 0) {
					session.flush();
					session.clear();
				}
			}
			session.flush();
			session.clear();
		} catch (Exception e) {
			e.printStackTrace();
			Log.error(e.getMessage());
		}
	}*/
}
